$.Class("Test.Grid",
// static properties
{         
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/test/search",
                        dataType: "json",
                        type: "post",
                        data : {
                            
                        }
                    },
					update: {
						url: "/test/update",
						dataType: "json",
						type: "post",
					},
					create: {
						url: "/test/create",
						dataType: "json",
						type: "post"						
					},
					destroy: {
						url: "/test/delete",
						dataType: "json",
						type: "post"
					},
					parameterMap: function(options, operation) {						
					
						if (operation == "create" || operation == "update"){
							if($("#startDate").val() == "") delete(options.models[0].startDate);
							else options.models[0].startDate = $("#startDate").val();
							if($("#endDate").val() == "") delete(options.models[0].endDate);										
							else options.models[0].endDate = $("#endDate").val();
							if($("#startDomain").val() == "") delete(options.models[0].startDomain);										
							else options.models[0].startDomain = $("#startDomain").val();
							if($("#endDomain").val() == "") delete(options.models[0].endDomain);										
							else options.models[0].endDomain = $("#endDomain").val();
							if($("#joinedDate").val() == "") delete(options.models[0]
							.joinedDate);										
							else options.models[0].joinedDate = $("#joinedDate").val();
							//return {models: kendo.stringify(options.models)};
							
							delete(options.models[0].joinedDateNumber);
							delete(options.models[0].rate);
							delete(options.models[0].lastPaid);
							delete(options.models[0].lastWithdraw);
							delete(options.models[0].duration);
							delete(options.models[0].grid_index);
						}
						
						return options;
					}
                },
				change: function (e, data) {
					if(e.action == "sync")
						$("#grid").data("kendoGrid").dataSource.read();	
				},
				error: function (xhr, error) {
					//console.debug(xhr); console.debug(error);
				},
				batch: true,
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    },
					model: {
						id: "id",
						fields: {
							id: { editable: false},
							domain: { validation: { required: true }},
							ip: { validation: { required: true }},
							planType: {defaultValue:'Very short'},
							profitType: {defaultValue:'Low'},
							status: {defaultValue:'New'},
							startDate: {},
							endDate: {},							
							startDomain: {},
							endDomain: {},
							joinedDate: {},				
							plan: {}
						}
					}
                },							
                requestStart: function(e) {                    
                }
            });

            return dataSource;
        }, 
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        toolbar: ["create","Reset"],
	        //height: 500,
	        filterable: false,
	        sortable: false,
	        pageable: true,
	        columns: [{
                field:"id",
                title:"ID",
                width: 30				
            },{
                field:"grid_index",
                title:" ",
                width: 40
            },{
                field:"domain",
                title:"Domain",
                width: 170,
                template: '<a href="" onclick="return showWindowPlan(${id});">${domain}</a>'
            },{
                field:"joinedDateNumber",
                title:"joinedDateNumber",
                width: 170,
				editable : false
            },{
            	title:"IP",
                field:"ip",
                width: 160  
            },{
            	title:"",
                field:"",         
                width: 70,
                template: '<a href="" onclick="return test.viewMonitor(${id});">Monitor</a>'
            },{
            	title:"",
                field:"",                
            },{ 
                command: ["edit","destroy"], title: "", width: 200 }
            ],
            editable: {
				mode: "popup",
				template: $("#teamEditorTemplate").html(),
				update: true,
				destroy: true,				
				confirmation: "Are you sure you want to remove this team member vh?",
				window:{
					width:730,
					center: true
				}
			},
            sortable:true,
            selectable: 'row',
            dataBound: function(e) {                
                $(".rate").each(function(){                   
                    var itemID = $(this).attr("itemID"); 
                    var rate = $(this).attr("value");
                    
                    if(rate == null) rate = 0;
                    
                    $('#' + this.id).raty({
                        half  : true,
                        score : rate,
                        click: function(score, evt) {
                            $.ajax({
                                type: 'post',
                                url: '/test/updateRate',
                                data: {
                                    id : itemID,
                                    rate : score
                                },
                                dataType: 'json',
                                success: function(obj){

                                },
                            });
                        }
                    });
                });
            }
        });	
        },
        initEvents : function(id)
        {
            $(".k-grid-Delete", "#" + this.id).click(function(){
                return false;
            });

            $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

Test.updateLastPaid = function(id)
{
    if(confirm("Have they paid ?"))
    {
        $.ajax({
            type: 'post',
            url: '/test/updateLastPaid',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Test.updateLastWithdraw = function(id)
{
    if(confirm("Have you received ?"))
    {
        $.ajax({
            type: 'post',
            url: '/test/updateLastWithdraw',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Test.viewMonitor = function(id)
{
    Monitortest.Grid.testID = id;
    tabs.data("kendoTabStrip").select(2);
    gridMonitor.refresh();
    
    return false;
}